<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="queryParams.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="hChange" />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          node-key="id"
          :data="depList"
          :props="defaultProps"
          :expand-on-click-node="false"
          default-expand-all
          highlight-current
          check-strictly
          @current-change="hCurrentChange"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button v-focus="'add'" size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog = true">excel导入</el-button>
          <!--  -->
          <el-button size="mini" @click="exportEmployee">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="employeeList">
          <el-table-column align="center" label="头像">
            <template v-slot="{row}">
              <img v-if="row.staffPhoto" style="width:30px" :src="row.staffPhoto" alt="">
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template v-slot="{row}">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>未知</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template v-slot="{row}">
              <el-button size="mini" type="text" @click="$router.push('/employee/detail/' + row.id)">查看</el-button>
              <el-button size="mini" type="text" @click="hRole(row.id)">角色</el-button>
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="确定删除？"
                @onConfirm="hDel(row.id)"
              >
                <el-button slot="reference" v-focus="'del'" type="text" size="small">删除</el-button>
              </el-popconfirm>
              <!-- <el-button size="mini" type="text" @click="delEmp(row.id)">删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            layout="total,prev, pager, next"
            :total="total"
            :page-size="queryParams.pagesize"
            :current-page="queryParams.page"
            @size-change="handleSizeChange"
            @current-change="hFenyeChange"
          />
        </el-row>
        <!-- 分页 -->
      </div>
    </div>
    <ImportExcel :show-excel-dialog.sync="showExcelDialog" @updateExcel="getDepartment" />

    <assign-role ref="refRole" :show-role-dialog.sync="showRoleDialog" :cod-id="codId" />
  </div>
</template>

<script>
import { getDepartment } from '@/api/department'
import { getEmployee, exportEmployee, delEmloyee } from '@/api/employee'
import FileSaver from 'file-saver'
import { transListToTreeData } from '@/utils/index'
import ImportExcel from './components/import-excel.vue'
import AssignRole from './components/assignRole.vue'
export default {
  name: 'Employee',
  components: { ImportExcel, AssignRole },
  data() {
    return {
      showRoleDialog: false,
      showExcelDialog: false, // 控制excel的弹层显示和隐藏
      depList: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      // 员工查询接口所需
      queryParams: {
        page: 1,
        pagesize: 10,
        keyword: null,
        departmentId: null
      },
      total: 0,
      // 员工列表数据
      employeeList: [],
      codId: 0
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    // 角色
    hRole(id) {
      this.codId = id
      this.showRoleDialog = true
      this.$nextTick(() => {
        this.$refs.refRole.getRolesUser()
      })
    },
    // 删除员工数据
    hDel(id) {
      delEmloyee(id)
      this.$message.success('删除成功!')
      if (this.employeeList.length === 1) {
        if (this.queryParams.page > 1) {
          this.page--
        }
      }
      this.getEmployee()
    },
    // 导出员工数据
    async  exportEmployee() {
      const result = await exportEmployee() // 导出所有的员工接口
      // console.log(result) // 使用一个npm包 直接将blob文件下载到本地 file-saver
      // FileSaver.saveAs(blob对象,文件名称)
      FileSaver.saveAs(result, '员工信息表.xlsx') // 下载文件
    },
    // 搜索查询
    hChange() {
      // 清除定时器
      clearTimeout(this.timerId)
      // 规定时间内   执行一次    防抖功能
      this.timerId = setTimeout(() => {
        // 页码设置为1
        this.queryParams.page = 1
        this.getEmployee()
      }, 300)
    },
    // 改变每页显示数量  从第一开始查询
    handleSizeChange(val) {
      this.queryParams.pagesize = val
      this.queryParams.page = 1
      this.getEmployee()
    },
    // 分页点击切换
    hFenyeChange(page) {
      this.queryParams.page = page
      this.getEmployee()
    },
    // 发送请求获取员工详情
    async getEmployee() {
      const res = await getEmployee(this.queryParams)
      console.log(res)
      this.employeeList = res.data.rows
      this.total = res.data.total
      console.log(this.employeeList, this.total)
    },
    // 选中后的事件,获取对应id 用于获取对应数据 树形菜单
    hCurrentChange(data, node) {
      this.queryParams.departmentId = data.id
      this.getEmployee()
    },
    // 获取组件架构
    async getDepartment() {
      const res = await getDepartment()
      // console.log(res)
      // 调用函数,数组转树状
      const rs = transListToTreeData(res.data, 0)
      this.depList = rs
      // 第一个节点
      const id = rs[0].id
      this.queryParams.departmentId = id
      this.$nextTick(() => {
        // 多选  待复选框的高亮
        // this.$refs.tree.setCheckedKeys([id])
        // 单个  选中高亮
        this.$refs.tree.setCurrentKey(id)
      })
      // 表格初始化数据
      this.getEmployee()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
